// pages/myrecord/myrecord.js

//index.js
//获取应用实例
const app = getApp();

var route = require('../../utils/route');
var config = require('../../config/config');
var httpClient = require('../../utils/httpClient');

Page({

  /**
   * 页面的初始数据
   */
  data: {
	  sent: null,
	  received: null,
	  currentTab:'0',
    sendlist: null,
    receivedlist: null,
	  showText:'共发出',
	  userInfo:null,
    size:10,
    sendCurrent: 1,
    sendTotalPage: 1,
    receivedCurrent: 1,
    receivedTotalPage: 1
    
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    this.getRecords(e.detail.current);
  },
  //点击切换
  clickTab: function (e) { 
	var showText  = '';
	if(e.target.dataset.current == 0 ){
		showText='共发出';
	}else{
		showText='共收到';
	}  
	this.setData({
		showText:showText,
    currentTab: e.target.dataset.current
	});
	this.getRecords(e.target.dataset.current);
    
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        that.setData({
          winHeight: calc
        });
      }
    });
    
    this.getRecords(this.data.currentTab);
    that.setData({
    	userInfo: app.globalData.userInfo
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRecords(this.data.currentTab);
  },

  getRecords: function (type) {
    var that = this;
    var method = '';
    var params = {
      userId: app.globalData.userId,
      size: that.data.size
    };
    if (type == 0) {
      method = route.getSendRecords();
    } else if (type == 1) {
      method = route.getReceivedRecords();
    }

    httpClient.request(method, 'GET', params, function (res, err) {
     
      if (type == 0) {
        that.setData({
          sendlist: res.rows,
          sent: res,
          sendCurrent: res.current + 1,
          sendTotalPage: res.pages
        });
      } else if (type == 1) {
        that.setData({
          receivedlist: res.rows,
          sent: res,
          receivedCurrent: res.current + 1,
          receivedTotalPage: res.pages
        });
      }

    });
  },




  getRecordsNext: function (type) {
	  var that=this;
	  var method = '';
    var params = '';
	  if(type==0){
      if (this.data.sendCurrent > this.data.sendTotalPage) {
        return;
      }
		  method = route.getSendRecords();
      params = {
        userId: app.globalData.userId,
        current: that.data.sendCurrent,
        size: that.data.size
      };
	  }else if(type==1){
      if (this.data.receivedCurrent > this.data.receivedTotalPage) {
        return;
      }
		  method = route.getReceivedRecords();
      params = {
        userId: app.globalData.userId,
        current: that.data.receivedCurrent,
        size: that.data.size
      };
	  } 

	  httpClient.request(method, 'GET', params, function (res, err) { 
      
      if (type == 0) {
        var data = that.data.sendlist;
        if (null != data) {
          var resList = data.concat(res.rows);
          that.setData({
            sendlist: resList,
          });
        } else {
          that.setData({
            sendlist: res.rows,
            sent: res
          });
        }
        if (that.data.sendCurrent <= res.pages) {
          that.setData({
            sendCurrent: res.current + 1,
            sendTotalPage: res.pages
          });
        }
       } else if (type == 1) {
        var data = that.data.receivedlist;
        if (null != data) {
          var resList = data.concat(res.rows);
          that.setData({
            receivedlist: resList,
          });
        } else {
          that.setData({
            receivedlist: res.rows,
            sent: res
          });
        }

          if (that.data.receivedCurrent <= res.pages) {
            that.setData({
              receivedCurrent: res.current + 1,
              receivedTotalPage: res.pages
            });
          }
        }

	  }); 
  },
  answer:function(){
	wx.navigateTo({
		url: '../question/question'
	})
  },
  redBagDtl:function(e){
	var redpacketnum = e.currentTarget.dataset.redpacketnum;
	wx.navigateTo({
    url: '../guesspacket/guesspacket?tradeNo='+redpacketnum
	})
  }, onReachBottom:function(){
    
    this.getRecordsNext(this.data.currentTab);
  }
})